import { defineStore } from 'pinia'
import settings from '@/config/settings'
import { TabNavItem, equalTabNavRoute } from '@/utils/routes';
import router from '@/config/routes';

export interface Global {
	// 左侧展开收起
	collapsed: boolean;
	// 顶部菜单开启
	topNavEnable: boolean;
	// 头部固定开启
	headFixed: boolean;
	// tab菜单开启
	tabNavEnable: boolean;
	// 头部tab导航列表
	headTabNavList: TabNavItem[];
}
const homeRoute = router.resolve(settings.homeRouteItem.path);

export const useGlobalStore = defineStore('global',  {
	state: () => {
		return {
			collapsed: false,
			topNavEnable: settings.topNavEnable,
			headFixed: settings.headFixed,
			tabNavEnable: settings.tabNavEnable,
			headTabNavList: [
				{
					route: homeRoute,
					menu: settings.homeRouteItem
				}
			]
		}
	},
	actions:{
		changeLayoutCollapsed(payload) {
		  this.collapsed = payload;
		},
		setTopNavEnable(payload) {
		  this.topNavEnable = payload;
		},
		setHeadFixed( payload) {
		  this.headFixed = payload;
		},
		setTabNavEnable(payload) {
		  this.tabNavEnable = payload;
		},
		setHeadTabNavList(payload) {
		  this.headTabNavList = payload;
		},
		/**
		 * 关闭当前tabNav，并跳转自定义路由
		 * @param state 
		 * @param payload RouteLocationRaw 跳转路由参数，必须
		 */
		 closeCurrentHeadTabNav(payload) {
		  const navList: TabNavItem[] =  this.headTabNavList.filter((item2: TabNavItem) => !equalTabNavRoute(router.currentRoute.value, item2.route, item2.menu.tabNavType))
		  this.headTabNavList = [
		    ...navList
		  ]
		
		  router.push(payload)
		},
	}
});